package cn.bat.slsystem.control;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;

import com.alibaba.fastjson.JSON;

import cn.bat.slsystem.pojo.AuRole;
import cn.bat.slsystem.service.RoleService;
import cn.bat.slsystem.tools.Constants;



@Controller
@RequestMapping(value="/role")
public class RoleController {
	@Autowired
	private RoleService roleService;
	
	
	@RequestMapping(value="/rolelist.html")
	public String list(HttpSession session){
		List<String> list = (List) session.getAttribute(Constants.lanjie);
		for (String path : list) {
			if(path.equals("role/rolelist.html")){
				return "rolelist";
			}
		}
		return "login";
	}
	/*显示所有角色信息*/
	@RequestMapping(value="/show.html")
	@ResponseBody
	public Object rolelist(Integer id){
		List<AuRole> list = roleService.showinfoRole(id);
		return list;
	}
	
	/*修改角色信息*/
	@RequestMapping(value="/update.html")
	public Object updaterole(AuRole role){
		System.out.println(role.getId()+"+++++++++=============>");
		role.setCreateddate(new Date());
		role.setCreatedby("admin");
		int count = roleService.updateRole(role);
		
		if(count>0){
			return "redirect:rolelist.html";
		}else{
			return "redirect:rolelist.html";
		}
	}
	/*是否启用角色信息*/
	@RequestMapping(value="/shifo.html")
	public void shifo(int id ,int isstart){
		 roleService.shifoRole(id, isstart);
	}
	
	/*根据id删除角色信息*/
	@RequestMapping(value="/delete.html")
	@ResponseBody
	public String del(int id){
		if(roleService.deleteRole(id)==1){
			return "ok";
		}
		return "no";
	}
	
	/*添加角色信息*/
	@RequestMapping(value="/add.html")
	public String addterole(AuRole role,HttpSession session){
		System.out.println(role.getRolename()+"+++++++++++++++++++++");
			role.setCreateddate(new Date());
			role.setCreatedby("admin");
			int count = roleService.addRole(role);
			if(count>0){
				session.setAttribute("show", "添加成功！");
				return "redirect:rolelist.html";
			}else{
				session.setAttribute("show", "添加失败！");
				return "redirect:rolelist.html";
			}
		
	}
}
